package com.powernode.lambda.stream;


import com.powernode.lambda.Fruit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Stream;

/**
 * 测试stream相关Api flatMap降维方法
 */
public class Stream02 {
    public static void main(String[] args) {

        ArrayList<Fruit> fruits1 = new ArrayList<>();
        fruits1.add(new Fruit("草莓", "Strawberry", "浆果", "红色"));
        fruits1.add(new Fruit("桑葚", "Mulberry", "浆果", "紫色"));
        fruits1.add(new Fruit("杨梅", "Waxberry", "浆果", "红色"));
        fruits1.add(new Fruit("蓝莓", "Blueberry", "浆果", "蓝色"));

        ArrayList<Fruit> fruits2 = new ArrayList<>();
        fruits2.add(new Fruit("核桃", "Walnut", "坚果", "棕色"));
        fruits2.add(new Fruit("草莓", "Peanut", "坚果", "棕色"));

        //stream流通过flatMap变成一个fruit对象
        Stream.of(

                fruits1,
                fruits2

        ).flatMap(List->List.stream()).forEach(System.out::println);

        Integer [][] arr = {
                {1,2,3},
                {4,5,6},
                {7,8,9},
        };

        //二维数组通过Arrays.stream(arr)变成一维数组，再通过flatMap变成单个元素
        Arrays.stream(arr).flatMap(List-> Arrays.stream(List)).forEach(System.out::println);
    }

}
